package com.yxhl.zoume.core.car.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.yxhl.zoume.R;
import com.yxhl.zoume.UIConstants;
import com.yxhl.zoume.common.ui.activity.base.BaseActivityWithToolBar;
import com.yxhl.zoume.core.car.fragment.common.SelectCityFragment;
import com.yxhl.zoume.core.func.map.ui.event.FinishLocationSelectEvent;
import com.yxhl.zoume.core.officialcar.fragment.OfficialCarLocationMapFragment;
import com.yxhl.zoume.core.officialcar.fragment.OfficialCarPOISearchFragment;
import com.yxhl.zoume.core.specialcar.model.CarLocationMapEntrance;

import rx.Subscription;
import rx.functions.Action1;

/**
 * Author：zhongshan   
 * Date：2016/12/06 18:59      
 * Description：地点选择页面
 */
public class CarLocationMapActivity extends BaseActivityWithToolBar {

    private final String FRAGMENT_OFFICIAL_CAR_LOCATION_MAP = "OfficialCarLocationMapFragment";
    private final String FRAGMENT_SELECT_CITY = "SelectCityFragment";
    private final String FRAGMENT_OFFICIAL_CAR_POI_SEARCH = "OfficialCarPOISearchFragment";

    private CarLocationMapEntrance entrance;
    private Subscription subscription;

    public static Intent getCallingIntent(Context context, CarLocationMapEntrance entrance) {
        Intent callingIntent = new Intent(context, CarLocationMapActivity.class);
        callingIntent.putExtra(UIConstants.LocationMap.ARGUMENT_KEY_LOCATION_MAP_FRAGMENT_ENTRANCE, entrance);
        return callingIntent;
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_car_loaction_container;
    }

    @Override
    protected void initializeInjector() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        onRxBusEvent();
        getParams();
        addFragment(R.id.car_location_activity_content, OfficialCarLocationMapFragment.newInstance(entrance), FRAGMENT_OFFICIAL_CAR_LOCATION_MAP);
    }

    @Override
    protected void onHeaderLeftClick(View view) {
        onBackPressed();
    }

    @Override
    protected void onHeaderRightClick(View view) {

    }

    private void getParams() {
        Intent intent = getIntent();
        entrance = (CarLocationMapEntrance) intent.getSerializableExtra(UIConstants.LocationMap.ARGUMENT_KEY_LOCATION_MAP_FRAGMENT_ENTRANCE);

    }

    private void onRxBusEvent() {
        subscription = toSubscription(FinishLocationSelectEvent.class,
                new Action1<FinishLocationSelectEvent>() {
                    @Override
                    public void call(FinishLocationSelectEvent event) {
                        finish();
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        onRxBusEvent();
                    }
                }
        );
        getCompositeSubscription().add(subscription);
    }

    private void unsubscribe() {
        if (subscription != null && subscription.isUnsubscribed())
            subscription.unsubscribe();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unsubscribe();
    }

    public void switchSelectCityFragment() {
        replaceFragment(getSupportFragmentManager().findFragmentByTag(FRAGMENT_OFFICIAL_CAR_LOCATION_MAP),
                SelectCityFragment.newInstance(entrance), R.id.car_location_activity_content, true, FRAGMENT_SELECT_CITY);
    }

    public void switchPOISearchFragment(){
        replaceFragment(getSupportFragmentManager().findFragmentByTag(FRAGMENT_OFFICIAL_CAR_LOCATION_MAP),
                OfficialCarPOISearchFragment.newInstance(entrance),R.id.car_location_activity_content,true,FRAGMENT_OFFICIAL_CAR_POI_SEARCH);
    }
}
